//
//  LGFillFollowOpinionController.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/6/26.
//  填写处理意见


#import "LGFillFollowOpinionController.h"

#define placeHolder  @"请输入..."

@interface LGFillFollowOpinionController ()<UITextViewDelegate>
/**   */
@property (nonatomic , strong)UIView *baseView;
/**   */
@property (nonatomic , strong)UITextView *textView;
/**   */
@property (nonatomic , strong)UIButton *saveButton;

@end

@implementation LGFillFollowOpinionController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"处理意见";
    self.content = (_content && _content.length>0)?self.content:@"";
    [self.view addSubview:self.baseView];
    [self.baseView addSubview:self.textView];
    [self.view addSubview:self.saveButton];
}

//保存
-(void)saveButtonAction{
    if (self.opinionFillFinished) {
        self.opinionFillFinished(self.content);
    }
    [self.navigationController popViewControllerAnimated:YES];
}


-(void)setContent:(NSString *)content{
    self.textView.text = content;
    if(content.length == 0 || [content isEqualToString:placeHolder]){
        self.textView.text = placeHolder;
        self.textView.textColor = [UIColor colorWithString:@"#999999"];
    }else{
        self.textView.textColor = [UIColor colorWithString:@"#333333"];
    }
    if(self.content.length == 0){
        [self textViewDidChange:self.textView];
    }
    _content = content;
}

- (void)textViewDidBeginEditing:(UITextView *)textView{
    self.textView.textColor = [UIColor colorWithString:@"#333333"];
    if([textView.text isEqualToString:placeHolder]){
        self.textView.text = @"";
    }
}
- (void)textViewDidEndEditing:(UITextView *)textView{
    self.content = textView.text;
}

- (void)textViewDidChange:(UITextView *)textView{
    NSString *lang = textView.textInputMode.primaryLanguage; // 键盘输入模式
    NSString *content = textView.text;
    if ([lang isEqualToString:@"zh-Hans"]){
        UITextRange *selectedRange = [textView markedTextRange];
        if (!selectedRange) { // 没有高亮
            if (textView.text.length>500) { // 输入的文字大于设定的长度  这里设置的是200个字符
                textView.text = [textView.text substringToIndex:500];
            }else{
                textView.text = textView.text;
            }
           
            if(textView.text.length>0){
                textView.attributedText = [textView.text attributeString:@{NSFontAttributeName:LGFont(14),NSForegroundColorAttributeName:[UIColor colorWithString:@"#333333"]} lineSpacing:5];
            }
            CGSize maxSize = CGSizeMake(self.textView.size.width, MAXFLOAT);
            CGSize fitSize = [textView sizeThatFits:maxSize];
            CGFloat viewHeight = self.textView.bounds.size.height;
            CGFloat newHeight = fitSize.height;
            NSLog(@">>>???>>>%.2f--%.2f",newHeight,viewHeight);
//            if(newHeight > viewPix(67) && newHeight != viewHeight){
//        //        NSLog(@">>>???>>>%.2f--%.2f",newHeight,viewHeight);
//                [self.textView mas_updateConstraints:^(MASConstraintMaker *make) {
//                    make.height.equalTo(@(newHeight));
//                }];
//            }
        }
    }else{
        if (textView.text.length>500) {
            textView.text = [textView.text substringToIndex:500];
        }else{
            textView.text = textView.text;
        }
        if(textView.text.length>0){
            textView.attributedText = [textView.text attributeString:@{NSFontAttributeName:LGFont(14),NSForegroundColorAttributeName:[UIColor colorWithString:@"#333333"]} lineSpacing:5];
        }
        CGSize maxSize = CGSizeMake(Screen_W-viewPix(56), MAXFLOAT);
        CGSize fitSize = [textView sizeThatFits:maxSize];
        CGFloat viewHeight = self.textView.bounds.size.height;
        CGFloat newHeight = fitSize.height;
        NSLog(@">>>???>>>%.2f--%.2f",newHeight,viewHeight);
//        if(newHeight > viewPix(50) && newHeight != viewHeight){
//    //        NSLog(@">>>???>>>%.2f--%.2f",newHeight,viewHeight);
//            [self.textView mas_updateConstraints:^(MASConstraintMaker *make) {
//                make.height.equalTo(@(newHeight));
//            }];
//        }
    }
}


-(UIView *)baseView{
    if(!_baseView){
        _baseView = [UIView shadowViewWithColor:@"#B3C5D9" offset:CGSizeMake(0, 2) opacity:0.15 radius:15];
        _baseView.frame = CGRectMake(viewPix(16), viewPix(16), Screen_W-viewPix(32), viewPix(285));
        _baseView.backgroundColor = [UIColor whiteColor];
    }
    return _baseView;
}

-(UITextView *)textView{
    if(!_textView){
        _textView = [[UITextView alloc]initWithFrame:CGRectMake(viewPix(10), viewPix(5), Screen_W-viewPix(52), viewPix(275))];
        _textView.backgroundColor = [UIColor colorWithString:@"#FFFFFF"];
        _textView.tintColor = [UIColor colorWithString:@"#333333"];
        _textView.textColor = [UIColor colorWithString:@"#333333"];
        _textView.textAlignment = NSTextAlignmentLeft;
        _textView.font = LGFont(14);
        _textView.delegate = self;
    }
    return _textView;
}


-(UIButton *)saveButton{
    if(!_saveButton){
        _saveButton = [UIButton buttonWithTitle:@"保存" topMargin:Screen_H-topBarHeight-viewPix(80)];
        [_saveButton addTarget:self action:@selector(saveButtonAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _saveButton;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
